﻿using WQLWebAPI.Application.Images.Dtos;
using WQLWebAPI.Domain.Abstract;
using WQLWebAPI.Domain.Entities;
using WQLWebAPI.Infrastructure.CommonDtos;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace WQLWebAPI.Application.Images
{
    public class ImagesService : IImageService
    {
        private IBaseRepository<Image> _imagesRepository;
        private IMapper _mapper;

        public ImagesService(IBaseRepository<Image> imagesRepository, IMapper mapper)
        {
            _imagesRepository = imagesRepository;
            _mapper = mapper;


        }

        public async Task<Image> CreateAsync(byte[] value)
        {
            var image = new Image { ImageInfo = value };
            await _imagesRepository.AddAsync(image);
            await _imagesRepository.SaveChangesAsync();
            return image;
        }

        public async Task<bool> CreateManyAsync(List<byte[]> values)
        {
            await _imagesRepository.AddRangeAsync(values.Select(d => new Image { ImageInfo = d }));
            return await _imagesRepository.SaveChangesAsync()>0;
        }

        public async Task<Image> GetAsync(int id)
        {
            return await _imagesRepository.GetByIdAsync(id);
        }

        public async Task<List<Image>> GetAsync()
        {
            return await _imagesRepository.GetAll().ToListAsync();
        }

        public async Task<bool> RemovesAsync(List<int> id)
        {
            _imagesRepository.RemoveRange(_imagesRepository.GetAll().Where(d=>id.Contains(d.Id)));
            return await _imagesRepository.SaveChangesAsync() > 0;
        }

        public async Task<bool> UpdateAsync(int id, byte[] value)
        {
            var entity = await _imagesRepository.GetByIdAsync(id);
            if (entity != null)
            {
                entity.ImageInfo = value;
            }
            _imagesRepository.Update(entity);
            return await _imagesRepository.SaveChangesAsync() > 0;
        }

        public async Task<bool> UpdateManyAsync(List<ImageDto> values)
        {
            foreach (var item in values)
            {
                var entity = await _imagesRepository.GetByIdAsync(item.Id);
                if (entity != null)
                {
                    entity.ImageInfo = item.ImageInfo;
                }
                _imagesRepository.Update(entity);
            }
            return await _imagesRepository.SaveChangesAsync() > 0;

        }
        /// <summary>
        /// 将excel图文内容转为html对象
        /// </summary>
        /// <param name="excelContent">读取的excel内容</param>
        /// <param name="imgUrls">图片服务器地址</param>
        /// <returns></returns>
        public async Task<string> GetHtmlContent(ExcelContentDto excelContent, string imgUrls)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<p>" + excelContent.Content);
            //var urls = HttpContext.Request.Host.Value;
            if (excelContent.Images != null)
            {
                foreach (var item in excelContent.Images)
                {
                    var temp= await CreateAsync(item);
                    //sb.Append($"<img src={imgUrls}/api/Images/{temp.Id}>");
                    sb.Append($"<img src=api/Images/{temp.Id}>");
                }
                sb.Append("</p>");
            }
            else
            {
                sb.Append("</p>");
            }
            return sb.ToString();
        }
    }
}
